package com.bw.controller;

import com.bw.domain.Brand;
import com.bw.domain.Goods;
import com.bw.service.GoodsService;
import com.bw.vo.GoodsVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Controller
public class GoodsController {
    @Autowired
    GoodsService goodsService;


   //返回所有的品牌json数据
    @ResponseBody
    @GetMapping("listBrand")
    public List<Brand> listBrand(){
      return goodsService.listBrands();
    }



    @ResponseBody
    @RequestMapping("upFile")
    public String upFile(MultipartFile file) throws IOException {
      if(null!=file && !file.isEmpty()){
        //定义文件上传的路径
         String  path="c:/pic/";
         //a.png
         String originalFilename=file.getOriginalFilename();//获取文件的原始名称
          //为 了防止文件重名
          String filename = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
          File f = new File(path, filename);//构建空文件对象
          file.transferTo(f);//把上传的文件内容写入到硬盘
          return "/pic/"+filename ;
      }
        return null;
    }


    //批删
    @ResponseBody //把Java对象转为js对象
    @PostMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("gids[]") Integer[] gids){

        return goodsService.deleteBatch(gids)>0;
    }

   //单删
   @ResponseBody //把Java对象转为js对象
    @PostMapping("deleteById")
    public boolean deleteById(Integer id){

        return goodsService.deleteById(id)>0;
    }

    //修改-1  跳转到修改页面
    @GetMapping("update")
    public String update(Integer id,Model model){
        Goods goods = goodsService.get(id);//查询出要修改的对象
        model.addAttribute("goods",goods);//封装到model
     /*   List<Brand> listBrands = goodsService.listBrands();
        model.addAttribute("listBrands",listBrands);*/
        return "update";
    }


    //修改-2  执行修改
    @PostMapping("update")
    public String update(@Validated Goods goods,BindingResult result){
       if(result.hasErrors()){
            return "update";//则转发到增加页面，进行消息提示
        }

        if(goodsService.update(goods)>0)
            return "redirect:list"; //如果成功跳转到列表

        return "update";//如果失败 跳转到增加页面
    }


    //添加-1  跳转到添加页面
    @GetMapping("add")
    public String add(Model model){
        model.addAttribute("goods",new Goods());
        return "add";
    }


    //添加-2  执行添加
    //@Validated 对要封装的goods进行校验，BindingResult 如果校验不通过则封装消息提示信息到BindingResult
    @PostMapping("add")
    public String add(@Validated Goods goods, BindingResult result,Model model){
        if(result.hasErrors()){//如果有错误消息
          //  model.addAttribute("goods",goods);
          /*  List<Brand> listBrands = goodsService.listBrands();
            model.addAttribute("listBrands",listBrands);*/
            return "add";//则转发到增加页面，进行消息提示
        }


        if(goodsService.add(goods)>0)
            return "redirect:list"; //如果成功跳转到列表

        return "add";//如果失败 跳转到增加页面
    }



  //列表@RequestParam(defaultValue = "1")  如果不传值则默认1  如果传值则按传的参数取值
    @RequestMapping("list")
    public String list(GoodsVO goodsVO, @RequestParam(defaultValue = "1") Integer pageNum,  @RequestParam(defaultValue = "3")Integer pageSize, Model model){

        System.out.println("list.....................");
        PageInfo<Goods> info = goodsService.listGoods(goodsVO, pageNum, pageSize);

        model.addAttribute("info",info);
        model.addAttribute("goodsVO",goodsVO);//封装查询条件到model
      /*  List<Brand> listBrands = goodsService.listBrands();
        model.addAttribute("listBrands",listBrands);*/

        return "list";
    }


}
